1 package net.sf.bse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.StreamTokenizer;
30 import java.security.Security;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.Iterator;
34
35 import org.bouncycastle.jce.provider.BouncyCastleProvider;
36
37 /***
38 * Command-line entry point for application.
39 *
40 * @author Bill Foote (bill.foote@sun.com)
41 * @author Aleksi Peebles (aleksi.peebles@infocast.fi)
42 * @version $Revision: 1.4 $ $Date: 2004/06/24 08:30:03 $
43 */
44 public class BSE
45 {
46 private static HashMap arguments = new HashMap();
47
48 public static void main(String[] args)
49 {
50 System.out.println();
51 System.out.println("Broadcast Signing Engine, version 0.3.2");
52 System.out.println("Feedback to bse-users@lists.sourceforge.net");
53 System.out.println();
54
55 Security.addProvider(new BouncyCastleProvider());
56 String commandString;
57 if (args.length <= 0)
58 {
59 usage();
60 }
61 if (args[0].equals("-gui"))
62 {
63 if (args.length > 1)
64 {
65 usage();
66 return;
67 }
68 System.out.println("Starting GUI...\n");
69 new net.sf.bse.gui.BSEGUI().show();
70 return;
71 }
72 else if (args[0].equals("-args"))
73 {
74 if (args.length != 2)
75 {
76 usage();
77 return;
78 }
79 try
80 {
81 args = readArgs(args[1]);
82 }
83 catch (IOException e)
84 {
85 System.err.println("Error reading file \"" + args[1] + "\"");
86 System.err.println(e.getMessage());
87 System.err.println();
88 System.exit(2);
89 }
90 }
91 processArgs(args);
92 Command command = null;
93 if (args[0].equals("root"))
94 {
95 command = new GenerateRootCertificate(arguments);
96 }
97 else if (args[0].equals("request"))
98 {
99 command = new GenerateLeafRequest(arguments);
100 }
101 else if (args[0].equals("sign"))
102 {
103 command = new SignLeafCertificate(arguments);
104 }
105 else if (args[0].equals("xlet"))
106 {
107 command = new SignXlet(arguments);
108 }
109
110 if (command == null)
111 {
112 System.err.println();
113 System.err.println(
114 "The command \"" + args[0] + "\" is not recognized.");
115 usage();
116 }
117 else
118 {
119 validateArgs(command);
120 try
121 {
122 command.run();
123 }
124 catch (Exception ex)
125 {
126 System.err.println();
127 System.err.println("Execution failed:");
128 ex.printStackTrace(System.err);
129 System.err.println();
130 System.exit(1);
131 }
132 System.exit(0);
133 }
134 }
135
136 private static String[] readArgs(String fileName) throws IOException
137 {
138 ArrayList result = new ArrayList();
139 BufferedReader r = new BufferedReader(new FileReader(fileName));
140 StreamTokenizer st = new StreamTokenizer(r);
141 st.commentChar('#');
142 st.ordinaryChars('0', '9');
143 st.ordinaryChar('-');
144 st.ordinaryChar('.');
145 st.wordChars('0', '9');
146 st.wordChars('!', '!');
147 st.wordChars('$', '/');
148 st.wordChars('-', '-');
149 st.wordChars(':', '@');
150 st.wordChars('.', '.');
151 st.wordChars('[', '`');
152 st.wordChars('{', '~');
153 st.wordChars((char)128, (char)255);
154 while (true)
155 {
156 int res = st.nextToken();
157 if (res == StreamTokenizer.TT_EOF)
158 {
159 break;
160 }
161 else if (
162 res == StreamTokenizer.TT_WORD || res == '"' || res == '\'')
163 {
164 result.add(st.sval);
165 }
166 }
167 r.close();
168 return (String[])result.toArray(new String[result.size()]);
169 }
170
171 private static void processArgs(String[] args)
172 {
173 for (int i = 1; i < args.length;)
174 {
175 String key = args[i++];
176 if (i >= args.length)
177 {
178 System.err.println();
179 System.err.println("The attribute \"" + key +
180 "\" has no associated value. Other arguments:");
181 System.err.println(" command: " + args[0]);
182
183 for (Iterator it = arguments.keySet().iterator(); it.hasNext();)
184 {
185 Object att = it.next();
186 System.err.println(" attribute: " + att);
187 System.err.println(" value: " + arguments.get(att));
188 }
189 System.err.println();
190 System.exit(2);
191 }
192 else
193 {
194 String value = args[i++];
195 arguments.put(key, value);
196 }
197 }
198 }
199
200 private static String getArg(String key)
201 {
202 return (String)arguments.get(key);
203 }
204
205 public static void validateArgs(Command command)
206 {
207 String[] required = command.getRequiredArgs();
208 String[] optional = command.getOptionalArgs();
209 HashMap allArgs = new HashMap();
210 if (required != null)
211 {
212 for (int i = 0; i < required.length; i++)
213 {
214 allArgs.put(required[i], required[i]);
215 if (getArg(required[i]) == null)
216 {
217 System.err.println(
218 "Missing required argument \"" + required[i] + "\"\n");
219 command.usageMessage(System.err);
220 System.exit(1);
221 }
222 }
223 }
224
225 if (optional != null)
226 {
227 for (int i = 0; i < optional.length; i++)
228 {
229 allArgs.put(optional[i], optional[i]);
230 }
231 }
232 for (Iterator it = arguments.keySet().iterator(); it.hasNext();)
233 {
234 Object arg = it.next();
235 if (! allArgs.containsKey(arg))
236 {
237 System.err.println();
238 System.err.println("Unrecognized argument \"" + arg + "\"");
239 command.usageMessage(System.err);
240 System.exit(1);
241 }
242 }
243
244 }
245
246 private static void usage()
247 {
248 System.err.println(
249 "Usage: bse command arguments\n" +
250 " or: bse -args argfile\n" +
251 " or: bse -gui\n\n" +
252
253 "Commands:\n\n" +
254
255 " root Generate an MHP root certificate.\n" +
256 " request Generate a request for an MHP leaf certificate.\n" +
257 " sign Respond to a certificate request by signing an X509\n" +
258 " certificate.\n" +
259 " xlet Sign an Xlet.\n\n" +
260
261 "The arguments take the form of attribute/value pairs, like\n" +
262 "\"file: /tmp/myCert\". Used with -args, argfile must be a\n" +
263 "text file, containing the arguments in the above format.\n" +
264 "Quoted strings may appear in this file.\n"
265 );
266
267 System.exit(1);
268 }
269 }